package com.galudisu.handsonic.modules.system.dao;

import com.galudisu.handsonic.modules.system.domain.Resource;
import com.galudisu.handsonic.modules.system.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author Barudisshu
 */
@Repository
public interface ResourceDao extends JpaRepository<Resource, String> {

    @Query("select o.permission from Resource o where o.id in (?1)")
    Set<String> findPermissionsByResourceIds(Set<String> resourceIds);

    @Query("select r.permission from Resource r join r.roles o join o.users u where r.permission is not null and u" +
            ".username" + " = " + ":username")
    Set<String> findPermissionListByUsersUsername(@Param("username") String username);

    @Query("select r from Resource r where r.permission IN :permissions")
    List<Resource> findByPermissions(@Param("permissions") Set<String> permissions);

    @Query("select r.name from Resource r where r.id in :resourceIds")
    List<String> findNamesByContain(@Param("resourceIds") Collection<String> resourceIds);

    @Query("select distinct r from Resource r join r.roles o join o.users u where r.type = 'menu' and u = :user order by r.sequence asc")
    List<Resource> findMenuListByUser(@Param("user") User user);
}
